package com.zp.business.impl.diagnosis;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.api.diagnosis.ClinicRecord;
import com.zp.business.api.diagnosis.ClinicRecordBo;
import com.zp.business.api.diagnosis.ClinicRecordVo;
import com.zp.business.mapper.diagnosis.ClinicRecordMapper;
import com.zp.business.service.diagnosis.IClinicRecordService;
import com.zp.business.service.patient.IClinicMasterService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 患者病历表Service业务层处理
 *
 * @author zhang peng
 * @ date 2024-01-01
 */
@RequiredArgsConstructor
@Service
public class ClinicRecordServiceImpl implements IClinicRecordService {

    private final ClinicRecordMapper baseMapper;

    private final IClinicMasterService clinicMasterService;

    /**
     * 查询患者病历表
     */
    @Override
    public ClinicRecordVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询患者病历表列表(分页)
     */
    @Override
    public TableDataInfo<ClinicRecordVo> queryPageList (ClinicRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClinicRecord> lqw = buildQueryWrapper(bo);
        Page<ClinicRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询患者病历表列表(不分页)
     */
    @Override
    public List<ClinicRecordVo> queryList (ClinicRecordBo bo) {
        LambdaQueryWrapper<ClinicRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClinicRecord> buildQueryWrapper (ClinicRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClinicRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), ClinicRecord::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getPatientId()), ClinicRecord::getPatientId, bo.getPatientId());
        lqw.eq(StringUtils.isNotBlank(bo.getFabingriqi()), ClinicRecord::getFabingriqi, bo.getFabingriqi());
        lqw.eq(StringUtils.isNotBlank(bo.getZhusu()), ClinicRecord::getZhusu, bo.getZhusu());
        lqw.eq(StringUtils.isNotBlank(bo.getLiuxingbingshi()), ClinicRecord::getLiuxingbingshi, bo.getLiuxingbingshi());
        lqw.eq(StringUtils.isNotBlank(bo.getXianbingshi()), ClinicRecord::getXianbingshi, bo.getXianbingshi());
        lqw.eq(StringUtils.isNotBlank(bo.getJiwangshi()), ClinicRecord::getJiwangshi, bo.getJiwangshi());
        lqw.eq(StringUtils.isNotBlank(bo.getGerenshi()), ClinicRecord::getGerenshi, bo.getGerenshi());
        lqw.eq(StringUtils.isNotBlank(bo.getHunyushi()), ClinicRecord::getHunyushi, bo.getHunyushi());
        lqw.eq(StringUtils.isNotBlank(bo.getJiazushi()), ClinicRecord::getJiazushi, bo.getJiazushi());
        lqw.eq(StringUtils.isNotBlank(bo.getYuejingshi()), ClinicRecord::getYuejingshi, bo.getYuejingshi());
        lqw.eq(StringUtils.isNotBlank(bo.getAllergyHistory()), ClinicRecord::getAllergyHistory, bo.getAllergyHistory());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugAllergyHistory()), ClinicRecord::getDrugAllergyHistory, bo.getDrugAllergyHistory());
        lqw.eq(StringUtils.isNotBlank(bo.getFabinghouqingkuang()), ClinicRecord::getFabinghouqingkuang, bo.getFabinghouqingkuang());
        lqw.eq(StringUtils.isNotBlank(bo.getTiwen()), ClinicRecord::getTiwen, bo.getTiwen());
        lqw.eq(StringUtils.isNotBlank(bo.getMaibo()), ClinicRecord::getMaibo, bo.getMaibo());
        lqw.eq(StringUtils.isNotBlank(bo.getHuxi()), ClinicRecord::getHuxi, bo.getHuxi());
        lqw.eq(StringUtils.isNotBlank(bo.getXueya()), ClinicRecord::getXueya, bo.getXueya());
        lqw.eq(StringUtils.isNotBlank(bo.getSystolicPressure()), ClinicRecord::getSystolicPressure, bo.getSystolicPressure());
        lqw.eq(StringUtils.isNotBlank(bo.getDiastolicPressure()), ClinicRecord::getDiastolicPressure, bo.getDiastolicPressure());
        lqw.eq(StringUtils.isNotBlank(bo.getBingshichenshuzhe()), ClinicRecord::getBingshichenshuzhe, bo.getBingshichenshuzhe());
        lqw.eq(StringUtils.isNotBlank(bo.getTigejiancha()), ClinicRecord::getTigejiancha, bo.getTigejiancha());
        lqw.eq(StringUtils.isNotBlank(bo.getFuzhujiancha()), ClinicRecord::getFuzhujiancha, bo.getFuzhujiancha());
        lqw.eq(StringUtils.isNotBlank(bo.getXiaojiefenxi()), ClinicRecord::getXiaojiefenxi, bo.getXiaojiefenxi());
        lqw.eq(bo.getBingshicaijiriqi() != null, ClinicRecord::getBingshicaijiriqi, bo.getBingshicaijiriqi());
        lqw.eq(StringUtils.isNotBlank(bo.getDispose()), ClinicRecord::getDispose, bo.getDispose());
        lqw.eq(StringUtils.isNotBlank(bo.getSuggestion()), ClinicRecord::getSuggestion, bo.getSuggestion());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ClinicRecord::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ClinicRecord::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getSymptomaticType()), ClinicRecord::getSymptomaticType, bo.getSymptomaticType());
        lqw.eq(bo.getPrintDate() != null, ClinicRecord::getPrintDate, bo.getPrintDate());
        lqw.eq(StringUtils.isNotBlank(bo.getPrintUser()), ClinicRecord::getPrintUser, bo.getPrintUser());
        lqw.eq(StringUtils.isNotBlank(bo.getYiqing()), ClinicRecord::getYiqing, bo.getYiqing());
        lqw.eq(StringUtils.isNotBlank(bo.getFbg()), ClinicRecord::getFbg, bo.getFbg());
        lqw.eq(StringUtils.isNotBlank(bo.getHealthPreach()), ClinicRecord::getHealthPreach, bo.getHealthPreach());
        lqw.eq(StringUtils.isNotBlank(bo.getMmmtbzmldm()), ClinicRecord::getMmmtbzmldm, bo.getMmmtbzmldm());
        lqw.eq(StringUtils.isNotBlank(bo.getMmmtbzmc()), ClinicRecord::getMmmtbzmc, bo.getMmmtbzmc());
        lqw.eq(StringUtils.isNotBlank(bo.getAbzffbzdm()), ClinicRecord::getAbzffbzdm, bo.getAbzffbzdm());
        lqw.eq(StringUtils.isNotBlank(bo.getAbzffbzmc()), ClinicRecord::getAbzffbzmc, bo.getAbzffbzmc());
        return lqw;
    }

    /**
     * 新增患者病历表
     */
    @Override
    public Boolean insertByBo (ClinicRecordBo bo) {
        ClinicRecord add = BeanUtil.toBean(bo, ClinicRecord.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            //修改接诊医生doctor_id
            clinicMasterService.updateDoctorId(bo.getClinicId(), bo.getIsUpdateDoctorId());

        }
        return flag;
    }

    /**
     * 修改患者病历表
     */
    @Override
    public Boolean updateByBo (ClinicRecordBo bo) {
        ClinicRecord update = BeanUtil.toBean(bo, ClinicRecord.class);
        update.setUpdateBy(LoginHelper.getLoginUser().getNickName());
        update.setUpdateTime(new Date());
        validEntityBeforeSave(update);
        boolean b = baseMapper.updateById(update) > 0;
        if (b) {
            //修改接诊医生doctor_id
            clinicMasterService.updateDoctorId(bo.getClinicId(), bo.getIsUpdateDoctorId());
        }
        return b;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (ClinicRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除患者病历表
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
